<?php
require 'BaseController.php';

class Admin_PromotionController extends Admin_BaseController{
	
	
	public function init(){
		parent::init();
		$auth = Zend_Auth::getInstance();
		$this->_identity = $auth->getIdentity();
		$this->view->identity = $this->_identity;
		
	}
	public function indexAction(){
		
		$request = $this->getRequest();
		$message = '';
		if($request->isPost()){
			if(isset($_FILES['zip_file'])){
                        try{
                            $upload_dir = PUBLIC_PATH.DIRECTORY_SEPARATOR."khuyenmai".DIRECTORY_SEPARATOR; //your upload directory NOTE: CHMODD 0777
                            if(is_dir($upload_dir)){
                                
                                $open = opendir($upload_dir);
                                $file = readdir($open);
                                while($file !== false){
                                    chmod($upload_dir.$file, 0777);
                                    if(filetype($upload_dir.$file)== "dir"){
                                        $open1 = opendir($upload_dir.$file);
                                        $file1 = readdir($open1);
                                        while($file1 !== false){
                                            //chmod($upload_dir.$file.DIRECTORY_SEPARATOR.$file1, 0777);
                                            @unlink($upload_dir.$file.DIRECTORY_SEPARATOR.$file1);
                                            $file1 = readdir($open1);
                                        }
                                    }
                                   
                                    @unlink($upload_dir.$file);
                                    $file = readdir($open);
                                }
                                $filename = $_FILES['zip_file']['name']; //the filename

                                //move file
                                if(!move_uploaded_file($_FILES['zip_file']['tmp_name'], $upload_dir.$filename))
                                      $message = "There was a problem with the upload. Please try again.";
                                      $zip_dir = basename($filename, ".zip"); //get filename without extension fpr directory creation
                                        $archive = new Helper_Unzip($upload_dir.$filename);

                                        if ($archive->extract(PCLZIP_OPT_PATH, $upload_dir, PCLZIP_OPT_REPLACE_NEWER) == 0){
                                              $message="<span class='error'>Your .zip file can't unpacked</span>";
                                        }else{
                                              $message="<span class='success'>Upload file .zip complete!</span>";
                                        }



                                unlink($upload_dir.'/'.$filename); //delete uploaded file
                            }else{
                                $message = "<span class='error'>Upload director is error</span>";
                            }
                        }catch(Exception $ex){
                            echo $ex->getTraceAsString();
                            $message = "<span class='error'>Có lỗi trong quá trình up file</span>";
                        }
			
				$this->view->message = $message;
			}
		}
	}		
}